﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace WCSY.DAL
{
    public class fg_operatesAppService
    {
        public fg_operatesAppService()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string opNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from fg_operatesApp");
			strSql.Append(" where opNo='"+opNo+"' ");
			return DBHelper.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(WCSY.MODEL.fg_operatesApp model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.opNo != null)
			{
				strSql1.Append("opNo,");
				strSql2.Append("'"+model.opNo+"',");
			}
			if (model.opName != null)
			{
				strSql1.Append("opName,");
				strSql2.Append("'"+model.opName+"',");
			}
			if (model.remarks != null)
			{
				strSql1.Append("remarks,");
				strSql2.Append("'"+model.remarks+"',");
			}
			if (model.isApp != null)
			{
				strSql1.Append("isApp,");
				strSql2.Append(""+model.isApp+",");
			}
            if (model.isDefaultApp != null)
            {
                strSql1.Append("isDefaultApp,");
                strSql2.Append("" + model.isDefaultApp + ",");
            }
			strSql.Append("insert into fg_operatesApp(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
            int rows = DBHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public string Update(string opNo,int isAppchk,int isDefaultAppchk)
		{
            string sql = "";
            if (isAppchk == 1 && isDefaultAppchk == 1)
            {
                sql = " update fg_operatesApp set isApp=1,isDefaultApp=1 where opNo='" + opNo + "'";
            }
            else if (isAppchk == 1 && isDefaultAppchk == 0)
            {
                sql = " update fg_operatesApp set isApp=1 where opNo='" + opNo + "'";
                
            }
            else {
                sql = " update fg_operatesApp set isApp=0 where opNo='" + opNo + "'";
            }
            return sql;
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string opNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from fg_operatesApp ");
			strSql.Append(" where opNo='"+opNo+"' " );
			int rowsAffected=DBHelper.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string opNolist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from fg_operatesApp ");
			strSql.Append(" where opNo in ("+opNolist + ")  ");
			int rows=DBHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public WCSY.MODEL.fg_operatesApp GetModel(string opNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
            strSql.Append(" opNo,opName,remarks,isApp,isDefaultApp ");
			strSql.Append(" from fg_operatesApp ");
			strSql.Append(" where opNo='"+opNo+"' " );
			WCSY.MODEL.fg_operatesApp model=new WCSY.MODEL.fg_operatesApp();
			DataSet ds=DBHelper.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["opNo"]!=null && ds.Tables[0].Rows[0]["opNo"].ToString()!="")
				{
					model.opNo=ds.Tables[0].Rows[0]["opNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["opName"]!=null && ds.Tables[0].Rows[0]["opName"].ToString()!="")
				{
					model.opName=ds.Tables[0].Rows[0]["opName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["remarks"]!=null && ds.Tables[0].Rows[0]["remarks"].ToString()!="")
				{
					model.remarks=ds.Tables[0].Rows[0]["remarks"].ToString();
				}
				if(ds.Tables[0].Rows[0]["isApp"]!=null && ds.Tables[0].Rows[0]["isApp"].ToString()!="")
				{
					model.isApp=int.Parse(ds.Tables[0].Rows[0]["isApp"].ToString());
				}
                if (ds.Tables[0].Rows[0]["isDefaultApp"] != null && ds.Tables[0].Rows[0]["isDefaultApp"].ToString() != "")
                {
                    model.isDefaultApp = int.Parse(ds.Tables[0].Rows[0]["isDefaultApp"].ToString());
                }
				return model;
			}
			else
			{
				return null;
			}
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select opNo,opName,remarks,isApp,isDefaultApp ");
			strSql.Append(" FROM fg_operatesApp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" opNo,opName,remarks,isApp,isDefaultApp ");
			strSql.Append(" FROM fg_operatesApp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM fg_operatesApp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DBHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.opNo desc");
			}
			strSql.Append(")AS Row, T.*  from fg_operatesApp T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DBHelper.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
    }
}
